package chances.wechat.service.rdb;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import chances.wechat.entity.WechatUser;
import chances.wechat.service.WechatUserService;

@Service("userWechatService")
public class RdbWechatUserService extends BaseDao implements WechatUserService {

	@Override
	public WechatUser getWechatUser(String openId) {
		return this.findUser(openId);
	}

	public WechatUser findUser(String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("openId", openId);
		try {
			return (WechatUser) this.queryForObject("getWeixinUserByOpenId", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}

	}

	@Override
	public void insertWeixinUser(WechatUser user) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", user);
		try {
			this.insert("insertWeiXinUser", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@Override
	public void updateWeixinUserLoginTime(String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("openId", openId);
		paramsMap.put("time", new Date());
		try {
			this.update("updateWeixinUserLoginTime", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

}
